import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.JFileChooser;

public class SessionInfo {
	
	Vector<Account> accs;
	Vector<Proxy> proxys;
	ItemViewer accsPanel, proxysPanel;
	int accnum, proxnum;
	
	public SessionInfo(ItemViewer accsPanel, ItemViewer proxysPanel) {
		accs = new Vector<Account>();
		proxys = new Vector<Proxy>();
		this.proxysPanel = proxysPanel;
		this.accsPanel = accsPanel;
		accnum = 0;
		proxnum = 0;
	}
	
	public void loadAccs() {
		JFileChooser fc = new JFileChooser();
		String acc;
		Account temp;
		int result;
		File f;
		
		try {
			f = new File(new File(".").getCanonicalPath());
		    fc.setCurrentDirectory(f);
		} catch (IOException e) {
			e.printStackTrace();
		}

		result = fc.showOpenDialog(null);
		
        if ( result == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            try {
    		    BufferedReader acc_in = new BufferedReader(new FileReader(file.getPath()));
    		    
    		    temp = new Account();
    		    
    		    while ((acc = acc_in.readLine()) != null && acc.indexOf(":")!= -1) {
    		    	temp.login = acc.substring(0, acc.indexOf(":"));
    		    	temp.pass = acc.substring(acc.indexOf(":")+1);
    		        accs.add(temp);
    		    	accsPanel.addItem(acc);
    		        accnum++;
    		    }
    		    
    		    acc_in.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
        }
	}
	
	public void loadProxys() {
		JFileChooser fc = new JFileChooser();
		String proxy;
		Proxy temp;
		int result;
		File f;
		
		try {
			f = new File(new File(".").getCanonicalPath());
		    fc.setCurrentDirectory(f);
		} catch (IOException e) {
			e.printStackTrace();
		}

		result = fc.showOpenDialog(null);
		
        if ( result == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            try {
    		    BufferedReader acc_in = new BufferedReader(new FileReader(file.getPath()));
    		    
    		    temp = new Proxy();
    		    
    		    while ((proxy = acc_in.readLine()) != null && proxy.indexOf(":")!= -1) {
    		        temp.ip = proxy.substring(0, proxy.indexOf(":"));
    		        temp.port = Integer.valueOf(proxy.substring(proxy.indexOf(":")+1));
    		        proxys.add(temp);
    		        proxysPanel.addItem(proxy);
    		        proxnum++;
    		    }
    		    
    		    acc_in.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
        }
	}
	
	public synchronized Proxy getNextProxy() {
		Proxy p = new Proxy();
		return null;
	}
}
